<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pms:productAttribute:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pms:productAttribute:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pms:productAttribute:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pms:productAttribute:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <h3>{{ header }}</h3>

    <el-table v-loading="loading" :data="productAttributeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="属性录入方式" align="center" prop="inputType">
        <template slot-scope="scope">
          {{ scope.inputType === 0 ? '手工输入' : '从列表中选取' }}
        </template>
      </el-table-column>
      <el-table-column label="可选值列表" align="center" prop="inputList" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pms:productAttribute:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pms:productAttribute:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="属性录入方式" prop="inputType">
          <el-radio-group v-model="form.inputType">
            <el-radio v-for="option in inputTypeOptions" :key="option.value" :label="option.value">
              {{ option.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.inputType === 1" label="可选值列表" prop="inputList">
          <el-input type="textarea" v-model="form.inputList" placeholder="请输入可选值列表（一行输入一个）"
                    :autosize="{ minRows: 4, maxRows: 10}"/>
        </el-form-item>
        <el-form-item v-if="$route.query.type == 0" label="销售属性类型" prop="filterType">
          <el-radio-group v-model="form.filterType">
            <el-radio v-for="option in filterTypeOptions" :key="option.value" :label="option.value">
              {{ option.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" :min="0" v-model.number="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProductAttribute, getProductAttribute, delProductAttribute, addProductAttribute, updateProductAttribute, exportProductAttribute } from "@/api/pms/productAttribute";
import { unique } from "@/utils";

export default {
  name: "ProductAttribute",
  created() {
    this.queryParams.productAttributeCategoryId = this.productAttributeCategoryId;
    this.queryParams.type = this.$route.query.type;
    if (this.$route.query.type == 0) {
      this.rules.filterType = [
        { required: true, message: "销售属性类型不能为空", trigger: "change" }
      ];
    }
    this.getList();
  },
  computed: {
    productAttributeCategoryId() {
      return this.$route.query.id
    },
    header() {
      return this.$route.query.type == 0 ? '属性列表' : '参数列表';
    }
  },
  data() {
    return {
      attributeCategoryId: null,
      type: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品属性表格数据
      productAttributeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules:  {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        inputType: [
          { required: true, message: "属性录入方式不能为空", trigger: "change" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      },
      inputTypeOptions: [
        { label: '手工录入', value: 0 },
        { label: '从列表中选取', value: 1 }
      ],
      filterTypeOptions: [
        { label: '普通', value: 0 },
        { label: '图片', value: 1 }
      ]
    };
  },
  methods: {
    /** 查询商品属性列表 */
    getList() {
      this.loading = true;
      listProductAttribute(this.queryParams).then(response => {
        this.productAttributeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productAttributeCategoryId: null,
        name: null,
        inputType: null,
        inputList: null,
        sort: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProductAttribute(id).then(response => {
        this.form = response.data;
        this.form.inputList = this.form.inputList ? this.form.inputList.split(',').join('\n') : '';
        this.open = true;
        this.title = "修改商品属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.productAttributeCategoryId = this.productAttributeCategoryId;
          this.form.type = this.$route.query.type;
          this.form.inputList = this.form.inputList ? unique(this.form.inputList.split(/\s+/)).join(',') : '';
          if (this.form.id != null) {
            updateProductAttribute(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductAttribute(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品属性编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delProductAttribute(ids, this.productAttributeCategoryId, this.$route.query.type);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品属性数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductAttribute(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>